package com.gufe.service.user.impl.excellentperson;

import com.gufe.dto.excellentperson.ExcellentPersonnelDTO;
import com.gufe.entity.excellentperson.ExcellentImage;
import com.gufe.entity.excellentperson.ExcellentPersonnel;
import com.gufe.mapper.user.excellentperson.ExcellentImageMapper;
import com.gufe.mapper.user.excellentperson.ExcellentPersonnelMapper;
import com.gufe.result.Result;
import com.gufe.service.user.mp.excellentperson.ExcellentPersonnelService;
import com.gufe.vo.excellentperson.ExcellentPersonnelVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ExcellentPersonnelServiceImpl implements ExcellentPersonnelService {

    @Autowired
    private ExcellentPersonnelMapper personnelMapper;

    @Autowired
    private ExcellentImageMapper imageMapper;

    @Transactional
    @Override
    public Result<ExcellentPersonnelVO> createPersonnel(ExcellentPersonnelDTO personnelDTO) {
        ExcellentPersonnel personnel = new ExcellentPersonnel();
        BeanUtils.copyProperties(personnelDTO, personnel);
        if (personnelDTO.getTags() != null) {
            personnel.setTags(String.join(",", personnelDTO.getTags()));
        }
        personnelMapper.insert(personnel);

        if (personnelDTO.getImagePaths() != null && !personnelDTO.getImagePaths().isEmpty()) {
            imageMapper.insertBatch(personnel.getId(), personnelDTO.getImagePaths());
        }

        return getPersonnelById(personnel.getId());
    }

    @Override
    public Result<ExcellentPersonnelVO> getPersonnelById(Integer id) {
        ExcellentPersonnel personnel = personnelMapper.selectById(id);
        if (personnel == null) {
            return Result.error("人员信息不存在");
        }
        List<ExcellentImage> images = imageMapper.selectByPersonnelId(id);
        ExcellentPersonnelVO vo = convertToVO(personnel, images);
        return Result.success(vo);
    }

    @Transactional
    @Override
    public Result<ExcellentPersonnelVO> updatePersonnel(Integer id, ExcellentPersonnelDTO personnelDTO) {
        ExcellentPersonnel existingPersonnel = personnelMapper.selectById(id);
        if (existingPersonnel == null) {
            return Result.error("人员信息不存在");
        }

        ExcellentPersonnel personnel = new ExcellentPersonnel();
        BeanUtils.copyProperties(personnelDTO, personnel);
        personnel.setId(id);
        if (personnelDTO.getTags() != null) {
            personnel.setTags(String.join(",", personnelDTO.getTags()));
        }
        personnelMapper.updateById(personnel);

        if (personnelDTO.getImagePaths() != null) {
            imageMapper.deleteOldImages(id, personnelDTO.getImagePaths());
            List<ExcellentImage> existingImages = imageMapper.selectByPersonnelId(id);
            List<String> existingImagePaths = existingImages.stream().map(ExcellentImage::getImagePath).collect(Collectors.toList());
            List<String> newImagePaths = personnelDTO.getImagePaths().stream()
                    .filter(path -> !existingImagePaths.contains(path))
                    .collect(Collectors.toList());
            if (!newImagePaths.isEmpty()) {
                imageMapper.insertBatch(id, newImagePaths);
            }
        } else {
            imageMapper.deleteByPersonnelId(id);
        }

        return getPersonnelById(id);
    }

    @Transactional
    @Override
    public Result<Void> deletePersonnel(Integer id) {
        personnelMapper.deleteById(id);
        imageMapper.deleteByPersonnelId(id);
        return Result.success();
    }

    @Override
    public Result<List<ExcellentPersonnelVO>> getAllPersonnel() {
        List<ExcellentPersonnel> personnelList = personnelMapper.selectAll();
        List<ExcellentPersonnelVO> voList = personnelList.stream().map(personnel -> {
            List<ExcellentImage> images = imageMapper.selectByPersonnelId(personnel.getId());
            return convertToVO(personnel, images);
        }).collect(Collectors.toList());
        return Result.success(voList);
    }

    private ExcellentPersonnelVO convertToVO(ExcellentPersonnel personnel, List<ExcellentImage> images) {
        ExcellentPersonnelVO vo = new ExcellentPersonnelVO();
        BeanUtils.copyProperties(personnel, vo);
        if (!StringUtils.isEmpty(personnel.getTags())) {
            vo.setTags(Arrays.asList(personnel.getTags().split(",")));
        }
        vo.setImagePaths(images.stream().map(ExcellentImage::getImagePath).collect(Collectors.toList()));
        return vo;
    }
}